package com.dmagic.cms.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dmagic.cms.common.annotation.FormModel;
import com.dmagic.cms.common.annotation.RequestJsonParam;
import com.dmagic.cms.common.controller.CommonController;
import com.dmagic.cms.common.query.ConditionsBean;
import com.dmagic.cms.common.query.PageUtil;
import com.dmagic.cms.common.spring.SpringContextHolder;
import com.dmagic.cms.common.util.JsonUtil;
import com.dmagic.cms.entity.Company.Flag;
import com.dmagic.cms.entity.InquireInfo;
import com.dmagic.cms.service.BaseService;
import com.dmagic.cms.service.EmailService;
import com.dmagic.cms.service.InquireInfoService;

@Controller
@RequestMapping(value = "/inquire")
public class InquireInfoController extends CommonController {

	private String INQUIRE_INFO_LIST = "company/inquireInfoList";

	private String INQUIRE_INFO_VIEW = "company/inquireInfoView";
	static {
	}

	@Autowired
	private BaseService baseService;

	@Autowired
	private InquireInfoService inquireInfoService;
	
	@Autowired
	private EmailService email;
	
	@RequestMapping()
	public String list(Model model) {
		model.addAttribute("language", baseService.getAllLanguages());
		return INQUIRE_INFO_LIST;
	}

	@RequestMapping(value = "search", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String search(@RequestParam Integer pageNo, @RequestParam Integer pageSize,
			@RequestParam String orderType, @RequestParam String orderBy,
			@RequestJsonParam("rules") List<ConditionsBean> conditions) throws Exception {
		PageUtil<Map> pageUtil = new PageUtil<Map>();
		pageUtil.setAutoCount(true);
		pageUtil.setPageNo(pageNo);
		pageUtil.setPageSize(pageSize);
		pageUtil.setOrder(orderType);
		pageUtil.setOrderBy(orderBy);
		pageUtil.setConditions(conditions);

		inquireInfoService.search(pageUtil);
		return JsonUtil.toFullJsonWithDateFormat(pageUtil, DATE_FORMAT);
	}

	@RequestMapping("view/{inquireInfoId}")
	public String getInquireInfo(@PathVariable Long inquireInfoId, Model model) {
		model.addAttribute("inquireInfo", inquireInfoService.getInquireInfo(inquireInfoId));
		return INQUIRE_INFO_VIEW;
	}

	@RequestMapping("delete")
	@ResponseBody
	public String delete(@RequestJsonParam("inquireInfoIds") List<Long> inquireInfoIds) {
		inquireInfoService.deleteInquireInfo(inquireInfoIds);
		return "Delete Successful.";
	}

	@RequestMapping("delete/{inquireInfoId}")
	@ResponseBody
	public String delete(@PathVariable("inquireInfoId") Long inquireInfoId) {
		inquireInfoService.deleteInquireInfo(inquireInfoId);
		return "Delete Successful.";
	}
	
	@RequestMapping("save")
	public String save(@RequestParam String emailContent, @RequestParam String language, @FormModel("inquire") InquireInfo inquire, Model model) {
		inquireInfoService.saveInquireInfo(new InquireInfo[] {inquire});
		//EmailService emailService = SpringContextHolder.getBean("simpleMail");
		//emailService.sendMail(emailContent);
		email.sendMail(emailContent);
		model.addAttribute("language", language);
		return "web/contact/contact";
	}
	@InitBinder
	public void initBinder(WebDataBinder b) {
	}
}
